package com.keson.door2;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.arcsoft.face.ErrorInfo;
import com.arcsoft.face.FaceEngine;
import com.arcsoft.face.VersionInfo;
import com.keson.door2.util.Cache;
import com.keson.door2.util.CameraUtil;
import com.keson.door2.util.Constant;
import com.keson.door2.util.IDCardUtil;
import com.keson.door2.util.MqttClientUtil;
import com.keson.door2.util.TTSUtil;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    private FaceEngine faceEngine;
    public static Handler handler;

    @SuppressLint("HandlerLeak")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getPermission();
        setContentView(R.layout.activity_main);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); //隐藏状态栏

        faceEngine = new FaceEngine();

        faceEngine.activeOnline(this, "F2vsRqWFyT6HNgVdYPPPyi9hE7baEQfNxoqLN6VfmWfS", "Cf3a52X5UmMrvZHMxj95rtQSfd5YYFvPCBmHjNrQ5Qkx");

        int afCode = faceEngine.init(this, FaceEngine.ASF_DETECT_MODE_IMAGE, FaceEngine.ASF_OP_0_ONLY,
                16, 10, FaceEngine.ASF_FACE_RECOGNITION | FaceEngine.ASF_FACE_DETECT | FaceEngine.ASF_LIVENESS);
        VersionInfo versionInfo = new VersionInfo();
        faceEngine.getVersion(versionInfo);
        Log.i("?????????????????????????????", "initEngine:  init: " + afCode + "  version:" + versionInfo);

        if (afCode != ErrorInfo.MOK) {
            Toast.makeText(this, afCode+"", Toast.LENGTH_SHORT).show();
        }

        CameraUtil.create((SurfaceView)findViewById(R.id.surfaceView), (SurfaceView)findViewById(R.id.surfaceViewRect), this, faceEngine);

        Cache.initLocalFaceFeatureFromFile();

        IDCardUtil.init(this);

        TTSUtil.init(this);

        final ImageView photoView = (ImageView) findViewById(R.id.imageView);
        final TextView name = (TextView)findViewById(R.id.name);
        final TextView people = (TextView)findViewById(R.id.people);
        final TextView todo = (TextView)findViewById(R.id.todo);
        handler = new Handler(){
            @Override
            public void handleMessage(Message msg) {
                if(msg.what==100){
                    File file = new File(IDCardUtil.filepath + "/zp.bmp");
                    if(file.exists()){
                        photoView.setImageURI(Uri.fromFile(file));
                    }
                    name.setText("姓名："+Cache.IDCardInfo.get("name"));
                }else if(msg.what==99){
                    people.setText("接待人："+Cache.IDCardInfo.get("people"));
                    todo.setText("事由："+Cache.IDCardInfo.get("todo"));
                }else{
                    photoView.setImageResource(R.drawable.logo);
//                    Cache.IDCardInfo.clear();
                    name.setText("姓名：");
                    people.setText("接待人：");
                    todo.setText("事由：");
                }
            }
        };
        Constant.readConfig(this);
        MqttClientUtil.subscribeTopic(Constant.actionTopic, Constant.actionQos);
        MqttClientUtil.publishTopic(Constant.onlineTopic, Constant.onlineQos, Constant.onlineMsg,false);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CameraUtil.stopPreview();
                startActivity(new Intent(MainActivity.this, SettingActivity.class));
            }
        });
    }
    @Override
    protected void onResume() {
        super.onResume();
        CameraUtil.startPreview();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        IDCardUtil.unInit();
    }

    public void getPermission(){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.INTERNET
                    ,Manifest.permission.ACCESS_WIFI_STATE
                    ,Manifest.permission.CHANGE_WIFI_STATE
                    ,Manifest.permission.MOUNT_FORMAT_FILESYSTEMS
                    ,Manifest.permission.WRITE_EXTERNAL_STORAGE
                    ,Manifest.permission.READ_EXTERNAL_STORAGE
                    ,Manifest.permission.READ_LOGS
                    ,Manifest.permission.WAKE_LOCK
                    ,Manifest.permission.RECEIVE_BOOT_COMPLETED
                    ,Manifest.permission.WRITE_SETTINGS
                    ,Manifest.permission.WRITE_SECURE_SETTINGS
                    ,Manifest.permission.SET_TIME
                    ,Manifest.permission.SYSTEM_ALERT_WINDOW
                    ,Manifest.permission.CAMERA
                    ,Manifest.permission.RECORD_AUDIO
                    ,Manifest.permission.ACCESS_NETWORK_STATE
                    ,Manifest.permission.READ_PHONE_STATE
                    ,Manifest.permission.REORDER_TASKS}, 1);
        }
    }
}
